package com.memo.model;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.Html;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.memo.Eventbean.homeone_evetbean;
import com.memo.SQL.memosql;

import org.greenrobot.eventbus.EventBus;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class newedit_model {
    Activity activity;
    String datatime;
    String zi_match = "0字";
    time_match time_match;
    String thistime;

    public newedit_model(Activity activity) {
        this.activity = activity;
        time_match = new time_match();
    }

    //第一次进入编辑
    public void Start(TextView edit_activity_datatime, EditText edit_activity_content) {//点击+号后的操作
        Date date = new Date();
        SimpleDateFormat MMdd = new SimpleDateFormat("MM月dd日");
        SimpleDateFormat HH = new SimpleDateFormat("HH");
        SimpleDateFormat HHmm = new SimpleDateFormat("HH:mm");
        SimpleDateFormat yyyyMMdd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        thistime = yyyyMMdd.format(date);
        datatime = MMdd.format(date) + "|" + (time_match.gettime(Integer.valueOf(HH.format(date))) + (HHmm.format(date)) + "|");
        edit_activity_datatime.setText(datatime + zi_match);
        editlister(edit_activity_content,edit_activity_datatime);
    }

    //进行二次编辑
    public void loadStart(int id, memosql memosql, TextView textdatatime, EditText title, EditText content) throws ParseException {//从已编辑内容进来时的操作
        title.setText(getonlymemo(id, memosql, "title"));
        content.setText(Html.fromHtml(getonlymemo(id, memosql, "details").replace("&nbsp;",""), imgGetter, null));
        Date date = new Date();
        SimpleDateFormat MMdd = new SimpleDateFormat("MM月dd日");
        SimpleDateFormat HH = new SimpleDateFormat("HH");
        SimpleDateFormat HHmm = new SimpleDateFormat("HH:mm");
        SimpleDateFormat yyyyMMdd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date datenew = yyyyMMdd.parse(getonlymemo(id, memosql, "datatime"));

        datatime = MMdd.format(datenew) + "|" + (time_match.gettime(Integer.valueOf(HH.format(datenew))) + (HHmm.format(datenew)) + "|");
        zi_match = content.length() + "字";
        thistime = yyyyMMdd.format(date);
        textdatatime.setText(datatime + zi_match);
        editlister(content, textdatatime);
    }


    boolean isStartback = true;
    public void activityback(int id, memosql memosql, EditText edit_activity_title, EditText edit_activity_content) {//编辑后返回执行 判断内容在不为空时执行判断内容是否修改,如果修改就重新保存,并且排序拉至第一位,发送命令EventBus给UI,让UI刷新
        if (!edit_activity_content.getText().equals("")) {
            if (!getonlymemo(id,memosql,"title").equals(edit_activity_title.getText().toString()) || !getonlymemo(id,memosql,"details").equals(gethtmlString(edit_activity_content))) {
                memosql.getReadableDatabase().execSQL("DELETE  FROM memo WHERE id=" + id + ";");
                memosql.getReadableDatabase().execSQL("insert into memo values(null,'" + thistime + "','" + edit_activity_title.getText() + "','" + gethtmlString(edit_activity_content) + "')");
                Log.i("保存后的数据: ",gethtmlString(edit_activity_content));
                EventBus.getDefault().post(new homeone_evetbean());
                isStartback = false;
            }else {
                isStartback = false;
            }
            if (isStartback) {
                EventBus.getDefault().post(new homeone_evetbean());
                memosql.getReadableDatabase().execSQL("insert into memo values(null,'" + thistime + "','" + edit_activity_title.getText() + "','" + gethtmlString(edit_activity_content) + "')");
            }
        }
        memosql.close();
        activity.finish();
    }

    public void callGallery() {//使用Intent获取图片
        Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        intent.setType("image/*");
        activity.startActivityForResult(intent, 123);
    }

    Html.ImageGetter imgGetter = new Html.ImageGetter() {
        public Drawable getDrawable(String source) {
            Drawable drawable = null;
            drawable = Drawable.createFromPath(source); //显示本地图片
            drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable
                    .getIntrinsicHeight());
            return drawable;
        }
    };

    public String getonlymemo(int id,memosql memosql,String name){//按照ID获取详细数据
        String outname="";
        Cursor cursor = memosql.getReadableDatabase().rawQuery("SELECT * FROM memo WHERE id=" + id + ";", new String[]{});
        while (cursor.moveToNext()) {
            String ID = cursor.getString((cursor.getColumnIndex("id")));
            String DATATIME = cursor.getString((cursor.getColumnIndex("datatime")));
            String TITLE = cursor.getString((cursor.getColumnIndex("title")));
            String DETAILS = cursor.getString((cursor.getColumnIndex("details")));
            switch (name){
                case "id":
                    outname=ID;
                    break;
                case "datatime":
                    outname=DATATIME;
                    break;
                case "title":
                    outname=TITLE;
                    break;
                case "details":
                    outname=DETAILS;
                    break;
            }
        }
        return outname;
    }

    //编辑框内容
    public void editlister(EditText editText,TextView datatimetext){//编辑事件监听
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }
            @Override
            public void afterTextChanged(Editable s) {
                Date date = new Date();
                SimpleDateFormat MMdd = new SimpleDateFormat("MM月dd日");
                SimpleDateFormat HH = new SimpleDateFormat("HH");
                SimpleDateFormat HHmm = new SimpleDateFormat("HH:mm");
                SimpleDateFormat yyyyMMdd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                thistime = yyyyMMdd.format(date);
                datatime = MMdd.format(date) + "|" + (time_match.gettime(Integer.valueOf(HH.format(date))) + (HHmm.format(date)) + "|");

                zi_match = editText.length() + "字";
                datatimetext.setText(datatime + zi_match);
                if (editText.length() == 20000) {
                    Toast.makeText(activity, "字数超出限制,超出部分已截断!", Toast.LENGTH_LONG);
                }
            }
        });
        editText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("onClick: ","编辑框被点击"+editText.isFocusableInTouchMode());
                if (editText.isFocusableInTouchMode()==true){
                    editText.setFocusableInTouchMode(false);
                    editText.setCursorVisible(false);
                }else {

                    editText.setFocusableInTouchMode(true);
                    editText.setCursorVisible(true);
                }
            }
        });


    }
    public String gethtmlString(EditText editText){//EditText组件获取内容转换为Html
        return Html.toHtml(editText.getText()).replace("<p>","<span>").replace("<p","<span").replace("</p>","</span>");
    }

}
